package com.cuitrip.business.tripservice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.SuperFragmentUtil;
import android.view.KeyEvent;

import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;
import com.lab.app.BaseFragment;
import com.lab.jumper.BundleCenter;
import com.lab.jumper.Jumper;


/**
 * 行程编辑activity
 * Created by AK
 */
public class TripEditActivity extends BaseActivity {

    public static final String SERVICE_INFO_ID_KEY = "service_info_id";
    public static final String SERVICE_EDIT_MODE_KEY = "service_mode";
    private Bundle mFragmentBundle;
    //    private TripEditMainFragment mTripMainFragment;
    private SelectTripCatesFragment selectTripCatesFragment;
    private boolean isEdit;
    private ServiceInfo serviceInfo;


    public static void navigate2CreateTripActivity(Context context) {
        TrackUtil.trackScreenName("/insider/trips/create");
        Intent intent = new Intent(context, TripEditActivity.class);
        Jumper.launchActivity(context, intent);
    }

    public static void navigate2CreateTripActivity(Context context, boolean isEdit) {
        TrackUtil.trackScreenName("/insider/trips/create");
        Intent intent = new Intent(context, TripEditActivity.class);
        intent.putExtra(SERVICE_EDIT_MODE_KEY, isEdit);
        Jumper.launchActivity(context, intent);
    }

    public static void navigate2CreateTripActivity(Context context, Intent intent) {
        TrackUtil.trackScreenName("/insider/trips/create");
        Jumper.launchActivity(context, intent);
    }

//    public static void navigate2TripEditActivity(Context context, ServiceInfo serviceInfo, boolean isEdit) {
//        Intent intent = new Intent(context, TripEditActivity.class);
//        Bundle bundle = new Bundle();
//        bundle.putSerializable(ServiceInfo.KEY, serviceInfo);
//        bundle.putBoolean(SERVICE_EDIT_MODE_KEY, isEdit);
//        intent.putExtras(bundle);
//        Jumper.launchActivity(context, intent);
//    }

    public static void navigate2TripEditActivity(Context context, ServiceInfo serviceInfo, boolean isEdit) {
        Intent intent = new Intent(context, TripEditActivity.class);
        intent.putExtra(SERVICE_EDIT_MODE_KEY, isEdit);
        intent.putExtra(ServiceInfo.KEY, serviceInfo);
        Jumper.launchActivity(context, intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 移除BunderCenter原始Bundle,这里待优化,可能因业务需要而采取原始数据作为后续Bundle传入Fragment
        BundleCenter.getInstance().removeBundle(getIntent());
        if (null != getIntent()) {
            Intent intent = getIntent();
            isEdit = intent.getBooleanExtra(SERVICE_EDIT_MODE_KEY, false);
            if (intent.hasExtra(ServiceInfo.KEY)) {
                serviceInfo = (ServiceInfo) intent.getSerializableExtra(ServiceInfo.KEY);
            }
//            mFragmentBundle = intent.getExtras();
        }
        setContentView(R.layout.page_container_layout);
//        mTripMainFragment = new TripEditMainFragment();
//        mTripMainFragment.setArguments(mFragmentBundle);
//        openFragment(mTripMainFragment, R.id.container, TripEditMainFragment.class.getName());
        if (isEdit) {
            openFragment(SelectTripCatesFragment.newInstance(serviceInfo, false), R.id.container, SelectTripCatesFragment.class.getName());
        } else {
            openFragment(CreateTripEditMainFragment.newInstance(serviceInfo), R.id.container, CreateTripEditMainFragment.class.getName());

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            Fragment topFragment = SuperFragmentUtil.getCurrentTopFragment(getSupportFragmentManager());
            if (topFragment != null) {
                if (topFragment instanceof BaseFragment) {
                    if (((BaseFragment) topFragment).onBackPressed(true)) {
                        return true;
                    } else {
                        try {
                            getSupportFragmentManager().popBackStackImmediate();
                        } catch (Exception e) {
                        }
                        return true;
                    }
                } else {
                    try {
                        getSupportFragmentManager().popBackStackImmediate();
                    } catch (Exception e) {
                    }
                    return true;
                }
            } else {
                Fragment topAddedFragment = SuperFragmentUtil.getCurrentTopAddedFragment(getSupportFragmentManager());
                if (topAddedFragment != null) {
                    if (topAddedFragment instanceof CreateTripEditMainFragment) {
                        ((CreateTripEditMainFragment) topAddedFragment).onBackPressed(true);
                        return true;
                    } else {
                        return super.onKeyDown(keyCode, event);
                    }
                } else {
                    try {
                        getSupportFragmentManager().popBackStackImmediate();
                    } catch (Exception e) {
                    }
                    return true;
                }
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }


    @Override
    public void onBackPressed() {
//        if (selectTripCatesFragment.onBackPressed()) {
//
//        } else {
//            super.onBackPressed();
//        }
        super.onBackPressed();
    }

    public enum ContentMode {
        NEW_MODE, ContentMode, EDIT_MODE
    }
}
